<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="theme-color" content="#0078E7"><meta name="author" content="唐奇杭"><meta name="copyright" content="唐奇杭"><meta name="generator" content="Hexo 5.3.0"><meta name="theme" content="hexo-theme-yun"><title>zinx-学习-2.md | 奇坑的小网站</title><link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@900&amp;display=swap" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/star-markdown-css@0.1.22/dist/yun/yun-markdown.min.css"><script src="//at.alicdn.com/t/font_1140697_ed8vp4atwoj.js" async></script><script src="https://cdn.jsdelivr.net/npm/scrollreveal/dist/scrollreveal.min.js" defer></script><script>document.addEventListener("DOMContentLoaded", () => {
  [".post-card",".post-content img"].forEach((target)=> {
    ScrollReveal().reveal(target);
  })
});
</script><link rel="shortcut icon" type="image/svg+xml" href="/blog/yun.svg"><link rel="mask-icon" href="/blog/yun.svg" color="#0078E7"><link rel="alternate icon" href="/yun.ico"><link rel="preload" href="/blog/css/hexo-theme-yun.css" as="style"><link rel="preload" href="/blog/js/utils.js" as="script"><link rel="preload" href="/blog/js/hexo-theme-yun.js" as="script"><link rel="prefetch" href="/blog/js/sidebar.js" as="script"><link rel="preconnect" href="https://cdn.jsdelivr.net" crossorigin><link rel="stylesheet" href="/blog/css/hexo-theme-yun.css"><script id="yun-config">
    const Yun = window.Yun || {};
    window.CONFIG = {"hostname":"tangqihang.gitee.io","root":"/blog/","title":"奇坑的小网站","version":"1.3.0","mode":"auto","copycode":true,"anonymous_image":"https://cdn.jsdelivr.net/gh/YunYouJun/cdn/img/avatar/none.jpg","say":{"api":"https://v1.hitokoto.cn","hitokoto":true},"local_search":{"path":"/blog/search.xml"},"fireworks":{"colors":["102, 167, 221","62, 131, 225","33, 78, 194"]}};
  </script><meta name="description" content="Zinx学习日记">
<meta property="og:type" content="article">
<meta property="og:title" content="zinx-学习-2.md">
<meta property="og:url" content="https://tangqihang.gitee.io/blog/2021/08/12/zinx/zinx-%E5%AD%A6%E4%B9%A0-2/index.html">
<meta property="og:site_name" content="奇坑的小网站">
<meta property="og:description" content="Zinx学习日记">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2021-08-12T03:43:43.000Z">
<meta property="article:modified_time" content="2021-08-12T05:50:07.643Z">
<meta property="article:author" content="唐奇杭">
<meta property="article:tag" content="Zinx">
<meta name="twitter:card" content="summary"><script src="/blog/js/ui/mode.js"></script></head><body><script defer src="https://cdn.jsdelivr.net/npm/animejs@latest/anime.min.js"></script><script defer src="/blog/js/ui/fireworks.js"></script><canvas class="fireworks"></canvas><div class="container"><a class="sidebar-toggle hty-icon-button" id="menu-btn"><div class="hamburger hamburger--spin" type="button"><span class="hamburger-box"><span class="hamburger-inner"></span></span></div></a><div class="sidebar-toggle sidebar-overlay"></div><aside class="sidebar"><script src="/blog/js/sidebar.js"></script><ul class="sidebar-nav"><li class="sidebar-nav-item sidebar-nav-toc hty-icon-button sidebar-nav-active" data-target="post-toc-wrap" title="Table of Contents"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-list-ordered"></use></svg></li><li class="sidebar-nav-item sidebar-nav-overview hty-icon-button" data-target="site-overview-wrap" title="Overview"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-passport-line"></use></svg></li></ul><div class="sidebar-panel" id="site-overview-wrap"><div class="site-info fix-top"><a class="site-author-avatar" href="/blog/about/" title="唐奇杭"><img width="96" loading="lazy" src="/blog/22.png" alt="唐奇杭"></a><div class="site-author-name"><a href="/blog/about/">唐奇杭</a></div><a class="site-name" href="/blog/about/site.html">奇坑的小网站</a><sub class="site-subtitle">西郊有密林，助君出重围</sub><div class="site-desciption"></div></div><nav class="site-state"><a class="site-state-item hty-icon-button icon-home" href="/blog/" title="Home"><span class="site-state-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-home-4-line"></use></svg></span></a><div class="site-state-item"><a href="/blog/archives/" title="Archives"><span class="site-state-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-archive-line"></use></svg></span><span class="site-state-item-count">23</span></a></div><div class="site-state-item"><a href="/blog/categories/" title="Categories"><span class="site-state-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-folder-2-line"></use></svg></span><span class="site-state-item-count">9</span></a></div><div class="site-state-item"><a href="/blog/tags/" title="Tags"><span class="site-state-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-price-tag-3-line"></use></svg></span><span class="site-state-item-count">9</span></a></div><a class="site-state-item hty-icon-button" target="_blank" rel="noopener" href="https://yun.yunyoujun.cn" title="文档"><span class="site-state-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-settings-line"></use></svg></span></a></nav><hr style="margin-bottom:0.5rem"><div class="links-of-author"><a class="links-of-author-item hty-icon-button" rel="noopener" href="https://github.com/summer-boythink" title="GitHub" target="_blank" style="color:#6e5494"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-github-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" href="https://music.163.com/#/user/home?id=1754803981" title="网易云音乐" target="_blank" style="color:#C20C0C"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-netease-cloud-music-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" href="https://space.bilibili.com/277650245" title="哔哩哔哩" target="_blank" style="color:#FF8EB3"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-bilibili-line"></use></svg></a></div><hr style="margin:0.5rem 1rem"><div class="links"><a class="links-item hty-icon-button" href="/blog/links/" title="我的小伙伴们" style="color:dodgerblue"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-genderless-line"></use></svg></a></div><br><a class="links-item hty-icon-button" id="toggle-mode-btn" href="javascript:;" title="Mode" style="color: #f1cb64"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-contrast-2-line"></use></svg></a></div><div class="sidebar-panel sidebar-panel-active" id="post-toc-wrap"><div class="post-toc"><div class="post-toc-content"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#zinx0-8%E7%9A%84%E6%94%B6%E8%8E%B7"><span class="toc-number">1.</span> <span class="toc-text">zinx0.8的收获</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%85%B3%E4%BA%8ETCP%E6%8B%86%E5%8C%85%E8%A7%A3%E5%8C%85"><span class="toc-number">1.0.1.</span> <span class="toc-text">关于TCP拆包解包</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#TCP%E5%A4%9A%E8%B7%AF%E7%94%B1"><span class="toc-number">1.0.2.</span> <span class="toc-text">TCP多路由</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#1"><span class="toc-number">1.0.2.1.</span> <span class="toc-text">1</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#2"><span class="toc-number">1.0.2.2.</span> <span class="toc-text">2</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E8%AF%BB%E5%86%99%E5%88%86%E7%A6%BB"><span class="toc-number">1.0.3.</span> <span class="toc-text">读写分离</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#%E5%BC%80%E5%90%AF"><span class="toc-number">1.0.3.1.</span> <span class="toc-text">开启</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E8%AF%BB%E5%8F%96"><span class="toc-number">1.0.3.2.</span> <span class="toc-text">读取</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E5%86%99%E5%85%A5"><span class="toc-number">1.0.3.3.</span> <span class="toc-text">写入</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%B6%88%E6%81%AF%E9%98%9F%E5%88%97"><span class="toc-number">1.0.4.</span> <span class="toc-text">消息队列</span></a></li></ol></li></ol></li></ol></div></div></div><div class="tag-cloud"><div class="tag-cloud-tags"><a href="/blog/tags/GO/" style="font-size: 25.5px; color: #2680d4">GO</a> <a href="/blog/tags/JS/" style="font-size: 21px; color: #4d89c0">JS</a> <a href="/blog/tags/Python/" style="font-size: 12px; color: #999">Python</a> <a href="/blog/tags/Socket/" style="font-size: 12px; color: #999">Socket</a> <a href="/blog/tags/Vue/" style="font-size: 16.5px; color: #7391ad">Vue</a> <a href="/blog/tags/Zinx/" style="font-size: 16.5px; color: #7391ad">Zinx</a> <a href="/blog/tags/node/" style="font-size: 21px; color: #4d89c0">node</a> <a href="/blog/tags/%E4%BA%8C%E5%8F%89%E6%A0%91/" style="font-size: 12px; color: #999">二叉树</a> <a href="/blog/tags/%E9%9A%8F%E7%AC%94/" style="font-size: 30px; color: #0078e7">随笔</a></div></div></aside><main class="sidebar-translate" id="content"><div id="post"><article class="post-block" itemscope itemtype="https://schema.org/Article"><link itemprop="mainEntityOfPage" href="https://tangqihang.gitee.io/blog/blog/2021/08/12/zinx/zinx-%E5%AD%A6%E4%B9%A0-2/"><span hidden itemprop="author" itemscope itemtype="https://schema.org/Person"><meta itemprop="name" content="唐奇杭"><meta itemprop="description"></span><span hidden itemprop="publisher" itemscope itemtype="https://schema.org/Organization"><meta itemprop="name" content="奇坑的小网站"></span><header class="post-header"><h1 class="post-title" itemprop="name headline">zinx-学习-2.md</h1><div class="post-meta"><div class="post-time" style="display:inline-block"><span class="post-meta-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-calendar-line"></use></svg></span> <time title="Created: 2021-08-12 11:43:43" itemprop="dateCreated datePublished" datetime="2021-08-12T11:43:43+08:00">2021-08-12</time></div><div class="post-classify"><span class="post-category"> <span class="post-meta-item-icon" style="margin-right:3px;"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-folder-line"></use></svg></span><span itemprop="about" itemscope itemtype="https://schema.org/Thing"><a class="category" href="/blog/categories/Zinx/" style="--text-color:var(--hty-text-color)" itemprop="url" rel="index"><span itemprop="text">Zinx</span></a></span></span><span class="post-tag"><span class="post-meta-divider">-</span><a class="tag" href="/blog/tags/Zinx/" style="--text-color:var(--hty-text-color)"><span class="post-meta-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-price-tag-3-line"></use></svg></span><span class="tag-name">Zinx</span></a></span></div></div></header><section class="post-body" itemprop="articleBody"><div class="post-content markdown-body" style="--smc-primary:#0078E7;"><a id="more"></a>

<h2 id="zinx0-8的收获"><a href="#zinx0-8的收获" class="headerlink" title="zinx0.8的收获"></a>zinx0.8的收获</h2><h4 id="关于TCP拆包解包"><a href="#关于TCP拆包解包" class="headerlink" title="关于TCP拆包解包"></a>关于TCP拆包解包</h4><p>没什么好说的，自己基于此模拟了一套node的拆包封包，唯一缺点是没有写大小端<br><a target="_blank" rel="noopener" href="https://gitee.com/tangqihang/node_tcp_demo">代码点这</a></p>
<h4 id="TCP多路由"><a href="#TCP多路由" class="headerlink" title="TCP多路由"></a>TCP多路由</h4><h5 id="1"><a href="#1" class="headerlink" title="1"></a>1</h5><figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">   <span class="comment">// 1.服务端的使用 对于不同的ID使用不同方法</span></span><br><span class="line">   s.AddRouter(<span class="number">0</span>,&amp;PingRouter&#123;&#125;)</span><br><span class="line">s.AddRouter(<span class="number">1</span>,&amp;MyRouter&#123;&#125;)</span><br><span class="line">s.Serve()</span><br></pre></td></tr></table></figure>
<ul>
<li><p>AddRouter代码如下 会把 ID与router先绑定起来，等待日后调用</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">   <span class="comment">//1.判断路由对于ID是否存在</span></span><br><span class="line"><span class="keyword">if</span> _, ok := mh.Apis[msgId]; ok &#123;</span><br><span class="line">	<span class="built_in">panic</span>(<span class="string">&quot;repeat api,msgID=&quot;</span> + <span class="keyword">string</span>(msgId))</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//2.若是router不存在则添加路由</span></span><br><span class="line">mh.Apis[msgId] = router</span><br><span class="line">fmt.Println(<span class="string">&quot;Add msgID =&quot;</span>, msgId, <span class="string">&quot;success&quot;</span>)</span><br></pre></td></tr></table></figure>
<h5 id="2"><a href="#2" class="headerlink" title="2"></a>2</h5></li>
<li><p>在<code>connection.go</code>每一次接收到一个<code>request</code>后就会去执行<code>c.MsgHandler.DoMsgHandler(&amp;req)</code></p>
</li>
<li><p>DoMsgHandler()代码如下 他会去找这个request对应的路由ID，执行其对应的方法</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">   <span class="comment">//根据request找到对应ID</span></span><br><span class="line">handler, ok := mh.Apis[request.GetMsgId()]</span><br><span class="line"><span class="keyword">if</span> !ok &#123;</span><br><span class="line">	fmt.Println(<span class="string">&quot;api MsgId=&quot;</span>, request.GetMsgId(), <span class="string">&quot;is Not Found,Please register&quot;</span>)</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//根据这个ID执行对于路由</span></span><br><span class="line">handler.PreHandle(request)</span><br><span class="line">handler.Handle(request)</span><br><span class="line">handler.PostHandle(request)</span><br></pre></td></tr></table></figure>
<h4 id="读写分离"><a href="#读写分离" class="headerlink" title="读写分离"></a>读写分离</h4><h5 id="开启"><a href="#开启" class="headerlink" title="开启"></a>开启</h5><figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">   fmt.Println(<span class="string">&quot;Conn start() ...ConnId=&quot;</span>, c.ConnId)</span><br><span class="line"><span class="comment">//TODO 启动当前链接读数据的业务</span></span><br><span class="line"><span class="keyword">go</span> c.StartReader()</span><br><span class="line"><span class="comment">//TODO 启动当前链接写数据的业务</span></span><br><span class="line"><span class="keyword">go</span> c.StartWriter()</span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> &#123;</span><br><span class="line">	<span class="keyword">select</span> &#123;</span><br><span class="line">	<span class="keyword">case</span> &lt;-c.ExitChan: <span class="comment">//无法读取后会往ExitChan传入值</span></span><br><span class="line">		<span class="comment">//得到退出消息，不再阻塞</span></span><br><span class="line">		<span class="keyword">return</span></span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h5 id="读取"><a href="#读取" class="headerlink" title="读取"></a>读取</h5><figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">  <span class="comment">//用一个for阻塞，然后不断从此Conn中读取对应的数据，拆包</span></span><br><span class="line">  <span class="keyword">for</span>&#123;</span><br><span class="line">      <span class="keyword">if</span> _, err := io.ReadFull(c.GetTcpConnection(), headData); err != <span class="literal">nil</span> &#123;</span><br><span class="line">	fmt.Println(<span class="string">&quot;read msg head error&quot;</span>, err)</span><br><span class="line">	<span class="keyword">break</span></span><br><span class="line">&#125;</span><br><span class="line">  &#125;</span><br></pre></td></tr></table></figure>
<h5 id="写入"><a href="#写入" class="headerlink" title="写入"></a>写入</h5></li>
</ul>
<ol>
<li>首先是在用户调用<code>SendMsg()</code>中把封包好的数据放入msgChan中<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">    <span class="function"><span class="keyword">func</span> <span class="params">(c *Connection)</span> <span class="title">SendMsg</span><span class="params">(msgId <span class="keyword">uint32</span>, data []<span class="keyword">byte</span>)</span> <span class="title">error</span></span> &#123;</span><br><span class="line">	<span class="keyword">if</span> c.IsClose == <span class="literal">true</span> &#123;</span><br><span class="line">		<span class="keyword">return</span> errors.New(<span class="string">&quot;connection is closed&quot;</span>)</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">//将data进行封包</span></span><br><span class="line">	dp := NewDataPack()</span><br><span class="line"></span><br><span class="line">	binaryMsg, err := dp.Pack(NewMsgPackage(msgId, data))</span><br><span class="line">	<span class="keyword">if</span> err != <span class="literal">nil</span> &#123;</span><br><span class="line">		fmt.Println(<span class="string">&quot;Pack error msg id=&quot;</span>, msgId)</span><br><span class="line">		<span class="keyword">return</span> errors.New(<span class="string">&quot;pack error msg&quot;</span>)</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">//将数据发送给客户端</span></span><br><span class="line">	c.msgChan &lt;- binaryMsg</span><br><span class="line"></span><br><span class="line">	<span class="keyword">return</span> <span class="literal">nil</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li>之后<code>StartWriter</code>得到后用<code>Write</code>写入<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">    <span class="function"><span class="keyword">func</span> <span class="params">(c *Connection)</span> <span class="title">StartWriter</span><span class="params">()</span></span> &#123;</span><br><span class="line">	fmt.Println(<span class="string">&quot;[writer Goroutine is running]&quot;</span>)</span><br><span class="line">	<span class="keyword">defer</span> fmt.Println(c.RemoteAddr().String(), <span class="string">&quot;[conn Writer exit]&quot;</span>)</span><br><span class="line">	<span class="comment">//阻塞等待chan消息</span></span><br><span class="line">	<span class="keyword">for</span> &#123;</span><br><span class="line">		<span class="keyword">select</span> &#123;</span><br><span class="line">		<span class="keyword">case</span> data := &lt;-c.msgChan:</span><br><span class="line">			<span class="keyword">if</span> _, err := c.Conn.Write(data); err != <span class="literal">nil</span> &#123;</span><br><span class="line">				fmt.Println(<span class="string">&quot;Send data err&quot;</span>, err)</span><br><span class="line">				<span class="keyword">return</span></span><br><span class="line">			&#125;</span><br><span class="line">		<span class="keyword">case</span> &lt;-c.ExitChan:</span><br><span class="line">			<span class="comment">//表示Reader已经退出，此时Writer也要退出</span></span><br><span class="line">			<span class="keyword">return</span></span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="消息队列"><a href="#消息队列" class="headerlink" title="消息队列"></a>消息队列</h4></li>
</ol>
<ul>
<li>问题：每当处理一个router会开启一个go，数量一多，开启goroutine也会很多，所以为了避免goroutine切换带来的开销，我们需要写一个任务池<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//执行注册的路由方法</span></span><br><span class="line"><span class="keyword">go</span> c.MsgHandler.DoMsgHandler(&amp;req)</span><br></pre></td></tr></table></figure>
</li>
</ul>
<hr>
<ol>
<li>开启工作池<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//在开启server时候，开启worker工作池</span></span><br><span class="line"><span class="comment">//因为worker开启一次，所以和server一起只开启一次</span></span><br><span class="line"><span class="comment">//开启消息队列,worker工作池</span></span><br><span class="line">s.MsgHandler.StartWorkPool()</span><br></pre></td></tr></table></figure></li>
</ol>
<ul>
<li>StartWorkPool会开启10个Worker,这10个Worker都会等待一个chan里面有没有request传过来，有request会调用DoMsgHandler</li>
</ul>
<ol start="2">
<li>处理request<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span> utils.GlobalObject.MaxWorkerTaskLen &gt; <span class="number">0</span> &#123;</span><br><span class="line">    <span class="comment">//若是开启工作池,由工作池去处理</span></span><br><span class="line">    c.MsgHandler.SendMsgTaskQueue(&amp;req) <span class="comment">//把req发送到chan中</span></span><br><span class="line">&#125; <span class="keyword">else</span> &#123;</span><br><span class="line">    <span class="comment">//执行注册的路由方法</span></span><br><span class="line">    <span class="keyword">go</span> c.MsgHandler.DoMsgHandler(&amp;req)</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
</ol>
</div><div id="reward-container"><span class="hty-icon-button button-glow" id="reward-button" title="Donate" onclick="var qr = document.getElementById(&quot;qr&quot;); qr.style.display = (qr.style.display === &quot;none&quot;) ? &quot;block&quot; : &quot;none&quot;;"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-hand-coin-line"></use></svg></span><div id="reward-comment">I'm so cute. Please give me money.</div><div id="qr" style="display:none;"><div style="display:inline-block"><a target="_blank" rel="noopener" href="https://tang-webs.oss-cn-hangzhou.aliyuncs.com/tang-webs/202101012315352.jpg"><img loading="lazy" src="https://tang-webs.oss-cn-hangzhou.aliyuncs.com/tang-webs/202101012315352.jpg" alt="支付宝" title="支付宝"></a><div><span style="color:#00A3EE"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-alipay-line"></use></svg></span></div></div></div></div><ul class="post-copyright"><li class="post-copyright-author"><strong>Post author: </strong>唐奇杭</li><li class="post-copyright-link"><strong>Post link: </strong><a href="https://tangqihang.gitee.io/blog/2021/08/12/zinx/zinx-%E5%AD%A6%E4%B9%A0-2/" title="zinx-学习-2.md">https://tangqihang.gitee.io/blog/2021/08/12/zinx/zinx-%E5%AD%A6%E4%B9%A0-2/</a></li><li class="post-copyright-license"><strong>Copyright Notice: </strong>All articles in this blog are licensed under <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh" target="_blank" rel="noopener" title="CC BY-NC-SA 4.0 "><svg class="icon"><use xlink:href="#icon-creative-commons-line"></use></svg><svg class="icon"><use xlink:href="#icon-creative-commons-by-line"></use></svg><svg class="icon"><use xlink:href="#icon-creative-commons-nc-line"></use></svg><svg class="icon"><use xlink:href="#icon-creative-commons-sa-line"></use></svg></a> unless otherwise stated.</li></ul></section></article><div class="post-nav"><div class="post-nav-item"><a class="post-nav-prev" href="/blog/2022/02/24/go/unsafe-Sizeof/" rel="prev" title="unsafe.Sizeof"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-arrow-left-s-line"></use></svg><span class="post-nav-text">unsafe.Sizeof</span></a></div><div class="post-nav-item"><a class="post-nav-next" href="/blog/2021/08/08/%E6%9D%82/%E5%85%BD%E6%80%A7%E5%92%8C%E4%BA%BA%E6%80%A7-%E3%80%8A%E9%9D%A2%E7%BA%B1%E3%80%8B/" rel="next" title="兽性和人性--《面纱》"><span class="post-nav-text">兽性和人性--《面纱》</span><svg class="icon" aria-hidden="true"><use xlink:href="#icon-arrow-right-s-line"></use></svg></a></div></div></div><div id="comment"><div class="comment-tooltip text-center"><span>点击按钮跳转 GitHub Issues 评论。</span><br><span>若没有本文 Issue，您可以使用 Comment 模版新建。</span><br><a class="hty-button hty-button--raised" id="github-issues" target="_blank" rel="noopener" href="https://github.com/YunYouJun/yunyoujun.github.io/issues?q=is:issue+zinx-学习-2.md">GitHub Issues</a></div></div></main><footer class="sidebar-translate" id="footer"><div class="copyright"><span>&copy; 2019 – 2022 </span><span class="with-love" id="animate"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-cloud-line"></use></svg></span><span class="author"> 唐奇杭</span></div><div class="powered"><span>Powered by <a href="https://hexo.io" target="_blank" rel="noopener">Hexo</a> v5.3.0</span><span class="footer-separator">|</span><span>Theme - <a rel="noopener" href="https://github.com/YunYouJun/hexo-theme-yun" target="_blank"><span>Yun</span></a> v1.3.0</span></div></footer><a class="hty-icon-button" id="goUp" aria-label="back-to-top" href="#"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-arrow-up-s-line"></use></svg><svg class="progress-circle-container" viewBox="0 0 100 100"><circle class="progress-circle" id="progressCircle" cx="50" cy="50" r="48" fill="none" stroke="#0078E7" stroke-width="2" stroke-linecap="round"></circle></svg></a><a class="popup-trigger hty-icon-button icon-search" id="search" href="javascript:;" title="Search"><span class="site-state-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-search-line"></use></svg></span></a><script>window.addEventListener("DOMContentLoaded", () => {
  // Handle and trigger popup window
  document.querySelector(".popup-trigger").addEventListener("click", () => {
    document.querySelector(".popup").classList.add("show");
    setTimeout(() => {
      document.querySelector(".search-input").focus();
    }, 100);
  });

  // Monitor main search box
  const onPopupClose = () => {
    document.querySelector(".popup").classList.remove("show");
  };

  document.querySelector(".popup-btn-close").addEventListener("click", () => {
    onPopupClose();
  });

  window.addEventListener("keyup", event => {
    if (event.key === "Escape") {
      onPopupClose();
    }
  });
});
</script><script src="/blog/js/search/local-search.js" defer></script><div class="popup search-popup"><div class="search-header"><span class="popup-btn-close close-icon hty-icon-button"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-close-line"></use></svg></span></div><div class="search-input-container"><input class="search-input" id="local-search-input" type="text" placeholder="Searching..." value=""></div><div id="local-search-result"></div></div></div><script defer src="/blog/js/utils.js"></script><script defer src="/blog/js/hexo-theme-yun.js"></script></body></html>